package com.bite.spring.demo;

import com.bite.spring.demo.sample.MessageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-11-11
 * Time: 15:47
 */

@RequestMapping("/message")
@RestController
public class MessageController {
    //放到内存里
    private List<MessageInfo> messageInfoList = new ArrayList<>();

    //这里的"application/json"是什么意思？
//    @RequestMapping(value = "/publish")
    @RequestMapping(value = "/publish", produces = "application/json")
    //@RequestBody是请求正文获取数据，@ResponseBody是响应正文获取数据
    public String publish(@RequestBody MessageInfo messageInfo){
        if(!StringUtils.hasLength(messageInfo.getMessage())
            || !StringUtils.hasLength(messageInfo.getFrom())
            || !StringUtils.hasLength((messageInfo.getTo()))){
            return "{\"ok\" : 0}";
        }
        messageInfoList.add(messageInfo);
        return "{\"ok\" : 1}";
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageInfoList;
    }
}
